package com.mx.kn.cstf2.dao.impl;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import kn.cstf2.assign.Bean.ListaTablaBean;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.mx.cstf2.sql.MainSql;
import kn.mx.cstf2.sql.TransitionSql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mx.kn.cstf2.dao.MainDao;

@Repository
@Component("mainDao")
public class MainDaoImpl  implements MainDao{

	private JdbcTemplate jdbcStksf2;
	private JdbcTemplate jdbcCustomerSalesToolKit;

	@Autowired
	public void setDataSourceStkfsf2(@Qualifier("dataSourceStkfsf2") DataSource DataSourceStkfsf2) {
		this.jdbcStksf2 = new JdbcTemplate(DataSourceStkfsf2);
	}
	
	@Autowired
	public void setDataSourceCustomerSalesToolKit(@Qualifier("dataSourceCustomerSalesToolKit")
			DataSource dataSourceCustomerSalesToolKit) {
		this.jdbcCustomerSalesToolKit = new JdbcTemplate(dataSourceCustomerSalesToolKit);
		
	}

	
	public void updatedFalse(Integer knkey) {
	  
		try{
			int row=jdbcStksf2.update(MainSql.UPDATEFALSE,new Object[] {knkey});

		}catch(Exception e){
		
		}
	
	}


	public void updatedTrue(Integer knkey) throws SQLException {
		int row=jdbcStksf2.update( MainSql.UPDATETRUE,new Object[] {knkey});	
	}


	public void updatedTrue1(Integer knkey) throws SQLException {
		int row=jdbcStksf2.update(MainSql.UPDATE1TRUE,new Object[] {knkey});
	}
	
	
	public ArrayList<ListaTablaBean> getClientes() throws SQLException {
		ArrayList<ListaTablaBean> listaTable = new ArrayList<ListaTablaBean>();
		ArrayList<Integer> listaKnkeys = new ArrayList<Integer>();

		//checar el argumento del query
		List<Map<String, Object>> rows = jdbcCustomerSalesToolKit
				.queryForList(MainSql.SELECT_KNKEY);
		for (Map row : rows) {
			ListaTablaBean listaTablaBean = new ListaTablaBean();
			listaTablaBean.setKnkey((Integer) row.get("knkey"));
			listaTablaBean.setCompanyname((String) row.get("companyname"));
			listaTablaBean.setCreation((String)row.get("creationdate"));
			
			List<Map<String, Object>> restDatos = jdbcStksf2
					.queryForList(MainSql.SELECCIONAR_RESTDATA1, new Object[] { listaTablaBean.getKnkey() });
			for (Map datosThisProyect : restDatos) {
				listaTablaBean.setStatusTrans((String)datosThisProyect.get("status"));
				listaTablaBean.setStatusImple((String)datosThisProyect.get("statusimple"));
				listaTablaBean.setUpdated1((Boolean)datosThisProyect.get("updated1"));
			}
			listaTable.add(listaTablaBean);
			System.out.println("...");
		}

		return listaTable;
	}
	
	public ListaTablaBean getCliente(Integer knKey) throws SQLException {
		List<Map<String, Object>> rows = jdbcCustomerSalesToolKit
				.queryForList(MainSql.SELECT_KNKEY, new Object[] {knKey});
		ListaTablaBean listaTablaBean = new ListaTablaBean();
		for (Map row : rows) {
			
			listaTablaBean.setKnkey((Integer) row.get("knkey"));
			listaTablaBean.setCompanyname((String) row.get("companyname"));
			listaTablaBean.setCreation((String)row.get("creationdate"));
			
			List<Map<String, Object>> restDatos = jdbcStksf2
					.queryForList(MainSql.SELECCIONAR_RESTDATA1, new Object[] { listaTablaBean.getKnkey() });
			for (Map datosThisProyect : restDatos) {
				listaTablaBean.setStatusTrans((String)datosThisProyect.get("status"));
				listaTablaBean.setStatusImple((String)datosThisProyect.get("statusimple"));
				listaTablaBean.setUpdated1((Boolean)datosThisProyect.get("updated1"));
			}
		}

		return listaTablaBean;
	}
}
